package dao;

/**
 * 通用操作工具类
 * @author candy
 */
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class BaseDao {
	//驱动字符串
	private static String driver = "";
	//连接字符串
	private static String url = "";
	//账号
	private static String username = "";
	//密码
	private static String password = "";

	//连接对象
	private Connection conn;

	static {
		//在程序启动之前执行init方法
		init();
	}

	/**
	 * 初始化方法（在程序启动时执行）
	 * 读取配置文件，赋值给全局静态变量
	 */
	public static void init() {
		Properties params = new Properties();
		String configFile = "database.properties";
		InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);
		try {
			//加载配置文件
			params.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//读取配置数据
		driver = params.getProperty("driver");
		url = params.getProperty("url");
		username = params.getProperty("username");
		password = params.getProperty("password");

	}

	/**
	 * 获得连接方法
	 * @return
	 */
	public Connection getConnection() {
		try {
			//加载驱动
			Class.forName(driver);
			//获得连接
			conn = DriverManager.getConnection(url, username, password);
			System.out.println("------------"+conn);
			return conn;
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return conn;
	}

	/**
	 * 关闭通用方法
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
		//如果stmt不等于空，就关闭
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//如果rs不等于空，就关闭
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//如果不等于空，就关闭
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 增删改 通用方法
	 * @param sql
	 * @param param
	 * @return
	 */
	public int executeUpdate(String sql, Object... params) {
		PreparedStatement pstmt = null;
		conn = getConnection();
		try {
			//预编译sql语句
			pstmt = conn.prepareStatement(sql);
			//填充参数
			if (params != null) {
				for (int i = 0; i < params.length; i++) {
					pstmt.setObject(i + 1, params[i]);
				}
			}
			//执行
			return pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//关闭
			closeAll(conn, null, null);
		}
		return 0;
	}
	
	/**
     * 查询操作
     * 
     * @param sql
     *            sql语句
     * @param params
     *            参数数组
     * @return 查询结果集
     */
    protected ResultSet executeQuery(String sql, Object... params) {
    	System.out.println(123);
        conn = this.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

}
